package com.example.myapplication.tack_21;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.tack_21.adapter.CaseAdapter;
import com.example.myapplication.tack_21.adapter.FenLeiAdapter;
import com.example.myapplication.tack_21.adapter.WoDeAdapter;
import com.example.myapplication.tack_21.data.FenLeiData;
import com.example.myapplication.tack_21.data.WodeData;
import com.example.myapplication.utils.OkHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class PetHospitalActivity extends AppCompatActivity {
    private ImageView exit;
    private RecyclerView fenLei;
    private RecyclerView doctor;
    private TabLayout tab;
    private List<FenLeiData.DataDTO> listFenLei;
    private List<String> listTab;
    private List<WodeData.RowsDTO> listWoDe;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_21_activity_pet_hospital);
        init();
        exit_();
        getFenLei();
        getTab();
        getAnLie();
    }

    private void getTab() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                for (String s : listTab) {
                    TabLayout.Tab tb = tab.newTab();
                    tb.setText(s);
                    tab.addTab(tb);
                }
                tabAdapter();
            }
        });
    }

    private void tabAdapter() {
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if("我的问诊".equals(listTab.get(position))){
                    getWoDe();
                }else if("问诊案例".equals(listTab.get(position))){
                    getAnLie();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void getAnLie() {
        String url = "prod-api/api/pet-hospital/inquiry/list?pageNum=12&pageSize=10";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, WodeData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                WodeData anLieData = (WodeData)obj;
                if(anLieData.getCode() == 200){
                    listWoDe = anLieData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            CaseAdapter();
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void CaseAdapter() {
        CaseAdapter caseAdapter = new CaseAdapter(listWoDe, PetHospitalActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PetHospitalActivity.this);
        doctor.setLayoutManager(linearLayoutManager);
        doctor.setAdapter(caseAdapter);
    }


    private void getWoDe() {
        String url = "prod-api/api/pet-hospital/inquiry/my-list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, WodeData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                WodeData wd = (WodeData)obj;
                if(wd.getCode() == 200){
                    listWoDe = wd.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            wdAdapter();
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void wdAdapter() {
        WoDeAdapter woDeAdapter = new WoDeAdapter(listWoDe, PetHospitalActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PetHospitalActivity.this);
        doctor.setLayoutManager(linearLayoutManager);
        doctor.setAdapter(woDeAdapter);
    }

    private void getFenLei() {
        String url = "prod-api/api/pet-hospital/pet-type/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, FenLeiData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                FenLeiData fenLeiData = (FenLeiData)obj;
                if(fenLeiData.getCode() == 200){
                    listFenLei = fenLeiData.getData();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            fenAdapter();
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void fenAdapter() {
        FenLeiAdapter fenLeiAdapter = new FenLeiAdapter(listFenLei, PetHospitalActivity.this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(PetHospitalActivity.this,5);
        fenLei.setAdapter(fenLeiAdapter);
        fenLei.setLayoutManager(gridLayoutManager);
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PetHospitalActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        fenLei = findViewById(R.id.fen_lei);
        doctor = findViewById(R.id.doctor);
        tab = findViewById(R.id.tab);
        listTab = new ArrayList();

        listTab.add("问诊案例");
        listTab.add("我的问诊");
    }
}